//
//  NSMutableData+Replace.m
//  PrivateCloud
//
//  Created by mars on 13-10-18.
//  Copyright (c) 2013年 sarah doo. All rights reserved.
//

#import "NSMutableData+Replace.h"

@implementation NSMutableData (replace)
//-(void)replaceByString:(NSString *)str range:(NSRange)range length:(int)len offset:(int)off{
-(void)replaceByString:(NSString *)str length:(int)len offset:(unsigned long)off{
    
    Byte buffer [4];
    for (int i = 0; i < 4 ; i++) {
        buffer[i] = 0;
    }
    NSMutableData* endData = [NSMutableData dataWithBytes:buffer length:4];
    
    NSData* strData = [str dataUsingEncoding:NSUnicodeStringEncoding];
    
    NSMutableData* allData = [NSMutableData dataWithData:strData];
    [allData appendData:endData];
    
    NSRange testRange;
    testRange.location = 2;
    testRange.length = [allData length]-2;
    strData = [allData subdataWithRange:testRange];
    NSRange lenRange;
    lenRange.location = 0;
    lenRange.length = len;
    NSMutableData* strMuData = [[NSMutableData alloc]initWithCapacity:len];
    
    [strMuData replaceBytesInRange:lenRange withBytes:[strData bytes]];
    
    NSRange trueRange;
    trueRange.location = off;
    trueRange.length = len;
    
    [self replaceBytesInRange:trueRange withBytes:[strMuData bytes]];
    [strMuData release];
}

//-(void)replaceByString:(NSString *)str range:(NSRange *)r{
//    NSData* strData = [str dataUsingEncoding:NSUnicodeStringEncoding];
//    NSRange lenRange;
//    lenRange.location = 0;
//    lenRange.length =;
//    NSMutableData* strMuData = [[NSMutableData alloc]initWithCapacity:len];
//    [strMuData replaceBytesInRange:lenRange withBytes:[strData bytes]];
//    
//    NSRange trueRange;
//    trueRange.location = off;
//    trueRange.length = len;
//    
//    [self replaceBytesInRange:trueRange withBytes:[strMuData bytes]];
//}
@end
